package tetris.data;

public class Piece
{
    // The rows and cols minus 1
    public static int BOARD_ROWS = 19;
    public static int BOARD_COLS = 9;
    
    int[][] piece;
    int orient;
    int row;
    int col;
    
    public Piece(int orient, int row, int col)
    {
        this.orient = orient;
        this.row = row;
        this.col = col;
        initPiece();
    }

    public Piece(int row, int col)
    {
        this(0, row, col);
    }
    
    public Piece()
    {
        this(0, 5);
    }
    
    protected void initPiece()
    {
        piece = new int[5][5];
    }
    
    public void left()
    {
        col--;
    }
    
    public void right()
    {
        col++;
    }
    
    public void down()
    {
        row++;
    }
    
    public void lrotate()
    {
        transpose();
        rcols();
        orient = (orient + 1) % numOrients();
    }
    
    public void rrotate()
    {
        transpose();
        rrows();
        orient = (orient + numOrients() - 1) % numOrients();
    }
    
    private void rcols()
    {
        int temp = 0;
        for (int i = 0; i < piece[0].length; i++)
        {
            for (int j = 0; j < piece.length/2; j++)
            {
                temp = piece[j][i];
                piece[j][i] = piece[piece.length-j-1][i];
                piece[piece.length-j-1][i] = temp;
            }
        }
    }
    
    private void rrows()
    {
        int temp = 0;
        for (int i = 0; i < piece.length; i++)
        {
            for (int j = 0; j < piece[0].length/2; j++)
            {
                temp = piece[i][j];
                piece[i][j] = piece[i][piece[0].length-j-1];
                piece[i][piece[0].length-j-1] = temp;
            }
        }
    }
    
    private void transpose()
    {
        int temp = 0;
        for (int i = 0; i < piece.length; i++)
        {
            for (int j = i+1; j < piece.length; j++)
            {
                temp = piece[i][j];
                piece[i][j] = piece[j][i];
                piece[j][i] = temp;
            }
        }
    }
    
    public int numOrients()
    {
        return 1;
    }
    
    public boolean valid(int[][] board)
    {
        boolean result = true;
        for (int i = 0; i < piece.length && result; i++)
        {
            for (int j = 0; j < piece.length; j++)
            {
                if (piece[i][j] == 1 && 
                        (col + (j - 2) < 0 ||
                         col + (j - 2) > BOARD_COLS ||
                         row + (i - 2) < 0 ||
                         row + (i - 2) > BOARD_ROWS ||
                         board[row + (i - 2)][col + (j - 2)] == 1))
                {
                    result = false;
                    break;
                }
            }
        }
        return result;
    }
    
    public void print()
    {
        for (int i = 0; i < piece.length; i++)
        {
            for (int j = 0; j < piece[0].length; j++)
            {
                System.out.print(piece[i][j]);
            }
            System.out.println();
        }
    }
}
